<?php

require_once ( 'Zend/Controller/Action.php' );
require_once ( 'Gpc/User.php' );

class LoginController extends Zend_Controller_Action {

   public function indexAction() {

      if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) {
         
         $page = Zend_Registry::get( 'session' )->referalUrl;
         $username = strtolower( $this->getRequest()->getParam( 'username' ) );
         $password = $this->getRequest()->getParam( 'password' );
         
         $config = Zend_Registry::get( 'site_config' );
         
         if ( ! $username ) {
            $this->view->error = "No username supplied";
            return;
         }
         
         if ( ! $password ) {
            $this->view->error = "No password supplied";
            return;
         }
         
         $user = Gpc_User::factory( $config->forum->software );
         $result = $user->authenticate( $username, $password );
         
         if ( $result->isValid() ) {
            unset(Zend_Registry::get('session')->referalUrl);
            $this->_redirect( ! empty( $page ) ? $page : '/' );
         } else {
            $this->view->errror = "Invalid Login\n";
         }
      }
      
      $this->view->referalUrl = Zend_Registry::get( 'session' )->referalUrl;
   }
}
